package com.buba.aspect;


import com.buba.aspect.pojo.Log;
import com.buba.aspect.service.LogService;
import com.buba.customer.pojo.CustomerLogin;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

/**
 * 日志管理切面类   代理模式
 * 用于监控用户在项目中执行的所有动作
 */
public class LogAspect {
    private LogService logService;
    private HttpSession session;

    public void log(JoinPoint joinPoint) {
        Log log = new Log();
        CustomerLogin customerLogin = (CustomerLogin) session.getAttribute("customerLogin");
        // 第一登录时，session中没有登录对象，不去记录用户日志
        // 因为有一张单独记录用户登录的日志表
        if (customerLogin != null) {
            log.setLoginName(customerLogin.getLoginName());
//        获取封装了署名信息的对象,在该对象中可以获取到目标方法名,所属类的Class等信息
            log.setAction(joinPoint.getSignature().toString());
//        获取传入目标方法的参数对象
            System.out.println("-----" + Arrays.toString(joinPoint.getArgs()));
//        获取被代理的对象
            System.out.println("-----" + joinPoint.getTarget());
//        获取代理对象
            System.out.println("-----" + joinPoint.getThis());
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            log.setTime(dateFormat.format(new Date()));
            // 执行添加日志方法
            logService.addLog(log);
        }
    }

    public LogService getLogService() {
        return logService;
    }
    @Autowired
    public void setLogService(LogService logService) {
        this.logService = logService;
    }

    public HttpSession getSession() {
        return session;
    }
    @Autowired
    public void setSession(HttpSession session) {
        this.session = session;
    }
}
